using System.Collections;
using UnityEngine;

public class MusicManager : MonoBehaviour
{
	private static MusicManager instance;

	[SerializeField]
	public AudioSource source;

	public static MusicManager Instance => instance;

	private void Awake()
	{
		source = ((Component)this).GetComponent<AudioSource>();
		if ((Object)(object)instance != (Object)null && (Object)(object)instance != (Object)(object)this)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
			return;
		}
		instance = this;
		Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
	}

	private void Update()
	{
	}

	public static void PlaySong(AudioClip SongClip, bool FadePreviousSong, float FadeTime, AudioSource CustomSource = null)
	{
		if ((Object)(object)CustomSource == (Object)null)
		{
			CustomSource = instance.source;
		}
		if (!((Object)(object)SongClip == (Object)(object)CustomSource.clip) || !CustomSource.isPlaying)
		{
			if (FadePreviousSong)
			{
				((MonoBehaviour)instance).StartCoroutine(FadeToNewSong(FadeTime, SongClip, CustomSource));
				return;
			}
			CustomSource.clip = SongClip;
			CustomSource.Play();
		}
	}

	public static void PauseMusic()
	{
		if ((Object)(object)instance.source.clip != (Object)null)
		{
			instance.source.Pause();
		}
	}

	public static void ResumeMusic()
	{
		if ((Object)(object)instance.source.clip != (Object)null)
		{
			instance.source.UnPause();
		}
	}

	public static void StopSong(bool Fade, float FadeTime, AudioSource CustomSource = null)
	{
		if (Fade)
		{
			((MonoBehaviour)instance).StartCoroutine(FadeOut(FadeTime, CustomSource));
		}
		else if ((Object)(object)CustomSource != (Object)null)
		{
			CustomSource.Stop();
		}
		else
		{
			instance.source.Stop();
		}
	}

	public static IEnumerator FadeOut(float FadeTime, AudioSource CustomSource = null)
	{
		if ((Object)(object)CustomSource == (Object)null)
		{
			CustomSource = instance.source;
		}
		float startVolume = CustomSource.volume;
		while (CustomSource.volume > 0f)
		{
			AudioSource obj = CustomSource;
			obj.volume -= startVolume * Time.deltaTime / FadeTime;
			yield return null;
		}
		CustomSource.Stop();
		CustomSource.volume = startVolume;
	}

	public static IEnumerator FadeToNewSong(float FadeTime, AudioClip Song, AudioSource CustomSource = null)
	{
		if ((Object)(object)CustomSource == (Object)null)
		{
			CustomSource = instance.source;
		}
		float startVolume = CustomSource.volume;
		while (CustomSource.volume > 0f)
		{
			AudioSource obj = CustomSource;
			obj.volume -= startVolume * Time.deltaTime / FadeTime;
			yield return null;
		}
		yield return (object)new WaitForSeconds(0.1f);
		CustomSource.Stop();
		CustomSource.clip = Song;
		CustomSource.Play();
		while (CustomSource.volume < startVolume)
		{
			AudioSource obj2 = CustomSource;
			obj2.volume += startVolume * Time.deltaTime / FadeTime;
			yield return null;
		}
	}
}
